package com.encryption.logic.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.dao.Condition;
import com.encryption.dao.FileActionDAO;
import com.encryption.logic.FileActionService;
import com.encryption.model.FileAction;

/**
 * 文件操作接口接口实现类
 * 
 * @author Administrator
 * 
 */
public class FileActionServiceImpl implements FileActionService {

	private static final Logger log = LoggerFactory
			.getLogger(FileActionServiceImpl.class);

	private static FileActionServiceImpl fileActionServiceImpl = null;

	public static FileActionServiceImpl getFileActionServiceImpl() {
		if (null == fileActionServiceImpl) {
			fileActionServiceImpl = new FileActionServiceImpl();
		}
		return fileActionServiceImpl;
	}

	public List<FileAction> getAll() {
		return FileActionDAO.getFileActionDAO().findAll();
	}

	/**
	 * 添加文件操作
	 * 
	 * @param fileAction
	 */
	public boolean addFileAction(FileAction fileAction) {
		try {
			FileActionDAO.getFileActionDAO().save(fileAction);
		} catch (Exception e) {
			log.error("add fileAction is error", e);
			return false;
		}
		return true;
	}

	/**
	 * 编辑文件操作
	 * 
	 * @param fileAction
	 * @return
	 */
	public boolean updateFileAction(FileAction fileAction) {
		try {
			FileActionDAO.getFileActionDAO().merge(fileAction);
		} catch (Exception e) {
			log.error("modify fileAction is error", e);
			return false;
		}
		return true;
	}

	/**
	 * 删除文件操作
	 * 
	 * @param id
	 */
	public boolean deleteFileAction(String id) {
		try {
			FileActionDAO.getFileActionDAO().delete(
					FileActionDAO.getFileActionDAO().findById(
							Integer.parseInt(id)));
		} catch (Exception e) {
			log.error("delete fileAction is error", e);
			return false;
		}
		return true;
	}

	/**
	 * 查询文件操作列表
	 * 
	 * @return
	 */
	public List<FileAction> getFileActionByCondition(List<Condition> conditions) {
		return FileActionDAO.getFileActionDAO().findByExample(conditions);
	}

	/**
	 * 分页查询文件操作列表
	 * 
	 * @param instance
	 * @param page
	 * @return
	 */
	public List<FileAction> getFileActionPagingByCondition(
			List<Condition> condList, int page, int pageSize) {
		return FileActionDAO.getFileActionDAO().findByExample4Page(condList,
				page, pageSize);
	}

	/**
	 * 获取单个文件操作,根据id
	 * 
	 * @param userId
	 * @return
	 */
	public FileAction getFileActionById(String id) {
		return FileActionDAO.getFileActionDAO().findById(Integer.parseInt(id));
	}

	/**
	 * 根据名称获取文件操作
	 * 
	 * @param name
	 */
	public FileAction getFileActionByName(String name) {
		return (FileAction) (FileActionDAO.getFileActionDAO().findByName(name)
				.get(0));
	}

}
